%     AERO LOAD - программа для нагружения агрегатов самолета распределенной нагрузкой
%     Copyright (C) 2018-2024  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function Load = Precompute_Load_Data(Load)

% Load.Precomputed_Data.Span_Length
% Load.Precomputed_Data.Root_Chord_Length
% Load.Precomputed_Data.End_Chord_Length
% Load.Precomputed_Data.Span_Pressure_Center
% Load.Precomputed_Data.Linear_Load_Root
% Load.Precomputed_Data.Linear_Load_End
% Load.Precomputed_Data.Linear_Load_Difference
% Load.Precomputed_Data.Chord_Pressure_Center
% Load.Precomputed_Data.Root_Chord_Pressure_Center
% Load.Precomputed_Data.End_Chord_Pressure_Center

% Длина размаха (вычисляется позднее)
Load.Precomputed_Data.Span_Length = [];

% Длина корневой хорды
Load.Precomputed_Data.Root_Chord_Length = norm(Load.Chord_Surface.A1_projected - Load.Chord_Surface.A3_projected);

% Длина концевой хорды
Load.Precomputed_Data.End_Chord_Length = norm(Load.Chord_Surface.A2_projected - Load.Chord_Surface.A4_projected);

% Центр давления по размаху
switch Load.Span_Load.Law
    
    case 'CHORD_PROPORTIONAL'
        
        Load.Precomputed_Data.Span_Pressure_Center = 1/3 * (Load.Precomputed_Data.Root_Chord_Length + 2*Load.Precomputed_Data.End_Chord_Length) / ...
                                                           (Load.Precomputed_Data.Root_Chord_Length + Load.Precomputed_Data.End_Chord_Length);
        
    case 'TRAPEZOID'
        
        switch Load.Span_Load.Trapezoid_Zd_Type
            case 'PARAMETER'
                Load.Precomputed_Data.Span_Pressure_Center = Load.Span_Load.Trapezoid_Zd_Value;
            case 'LOCATION'
                Projected_Zd = Project_Point_To_Plane(Load.Span_Load.Trapezoid_Zd_Location, Load.Basic_Plane.Equation);
                Load.Precomputed_Data.Span_Pressure_Center = Get_Param_Coord(Load.Chord_Surface.A1_projected, ...
                                                                             Load.Chord_Surface.A2_projected, ...
                                                                             Load.Chord_Surface.A3_projected, ...
                                                                             Load.Chord_Surface.A4_projected, Projected_Zd);
        end
        
    case 'LOAD_DIAGRAM'
        
        Load.Precomputed_Data.Span_Pressure_Center = Center_Of_Mass(Load.Span_Load.Load_Diagram_Data);
        
end

% Погонная нагрузка на корневой хорде (вычисляется позднее)
Load.Precomputed_Data.Linear_Load_Root = [];

% Погонная нагрузка на концевой хорде (вычисляется позднее)
Load.Precomputed_Data.Linear_Load_End = [];

% Разница погонной нагрузки на корневой и на концевой хорде (вычисляется позднее)
Load.Precomputed_Data.Linear_Load_Difference = [];

% Центры давления по хордам
Load.Precomputed_Data.Chord_Pressure_Center = []; % (вычисляется, если постоянен по всему размаху)
Load.Precomputed_Data.Root_Chord_Pressure_Center = [];
Load.Precomputed_Data.End_Chord_Pressure_Center = [];

switch Load.Chord_Pressure.Law
    
    case 'TRAPEZOID'
        
        switch Load.Chord_Pressure.Trapezoid_Xd_Type
            case 'PARAMETER'
                Load.Precomputed_Data.Chord_Pressure_Center = Load.Chord_Pressure.Trapezoid_Xd_Value;
                Load.Precomputed_Data.Root_Chord_Pressure_Center = Load.Chord_Pressure.Trapezoid_Xd_Value;
                Load.Precomputed_Data.End_Chord_Pressure_Center = Load.Chord_Pressure.Trapezoid_Xd_Value;
                
            case 'LOCATION'
                Projected_Xd = Project_Point_To_Plane(Load.Chord_Pressure.Trapezoid_Xd_Location, Load.Basic_Plane.Equation);
                Load.Precomputed_Data.Chord_Pressure_Center = Get_Param_Coord(Load.Chord_Surface.A1_projected, ...
                                                                              Load.Chord_Surface.A3_projected, ...
                                                                              Load.Chord_Surface.A2_projected, ...
                                                                              Load.Chord_Surface.A4_projected, Projected_Xd);
                Load.Precomputed_Data.Root_Chord_Pressure_Center = Load.Precomputed_Data.Chord_Pressure_Center;
                Load.Precomputed_Data.End_Chord_Pressure_Center = Load.Precomputed_Data.Chord_Pressure_Center;
                
            case 'DISTRIBUTION'
                Load.Precomputed_Data.Root_Chord_Pressure_Center = Access_Array(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data, 0, ...
                                                                       Load.Extrapolation.Trapezoid_Xd_0, Load.Extrapolation.Trapezoid_Xd_1);
                Load.Precomputed_Data.End_Chord_Pressure_Center = Access_Array(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data, 1, ...
                                                                       Load.Extrapolation.Trapezoid_Xd_0, Load.Extrapolation.Trapezoid_Xd_1);
                
                if (all(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(:,2) == Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(1,2)))
                    Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
                end
        end
        
    case {'MODEL_1', 'MODEL_3', 'MODEL_15'}
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_p2_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case 'MODEL_2'
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_p2_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_p3_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case {'MODEL_4', 'MODEL_10'}
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_f_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case 'MODEL_5'
        
        Load.Precomputed_Data.Chord_Pressure_Center = 1/3;
        Load.Precomputed_Data.Root_Chord_Pressure_Center = 1/3;
        Load.Precomputed_Data.End_Chord_Pressure_Center = 1/3;
        
    case {'MODEL_6', 'MODEL_8', 'MODEL_14'}
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_p2_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f2_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f2_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f2_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case {'MODEL_7', 'MODEL_11', 'MODEL_13'}
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_p2_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_p3_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f2_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f2_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f2_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case 'MODEL_9'
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_f1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f2_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f2_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f2_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case 'MODEL_12'
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_Model_X(1, Load));
        
        Constant_Pressure_Center_Flag = 1;
        
        if (strcmp(Load.Chord_Pressure.Model_X_p2_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_p3_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_p4_p1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f1_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f1_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f1_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (strcmp(Load.Chord_Pressure.Model_X_f2_Type, 'DISTRIBUTION'))
            if (~all(Load.Chord_Pressure.Model_X_f2_Distr_Data(:,2) == Load.Chord_Pressure.Model_X_f2_Distr_Data(1,2)))
                Constant_Pressure_Center_Flag = 0;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
        
    case 'USER_MODEL'
        
        Load.Precomputed_Data.Root_Chord_Pressure_Center = Center_Of_Mass(Access_User_Model(0, Load));
        Load.Precomputed_Data.End_Chord_Pressure_Center = Center_Of_Mass(Access_User_Model(1, Load));
        
        tmp = Load.Chord_Pressure.User_Model_Data.Pressure_Data;
        
        for i=1:size(tmp,1)
            tmp{i}(:,2) = tmp{i}(:,2) / trapz(tmp{i}(:,1), tmp{i}(:,2));
        end
        
        Constant_Pressure_Center_Flag = 1;
        
        for i=2:size(tmp,1)
            if (~isequal(tmp{i}(:,2), tmp{1}(:,2)))
                Constant_Pressure_Center_Flag = 0;
                break;
            end
        end
        
        if (Constant_Pressure_Center_Flag == 1)
            Load.Precomputed_Data.Chord_Pressure_Center = Load.Precomputed_Data.Root_Chord_Pressure_Center;
        end
    
end

% Длина размаха
End_Chord_PC = Load.Precomputed_Data.End_Chord_Pressure_Center;

if (End_Chord_PC < 0)
    End_Chord_PC = 0;
end

if (End_Chord_PC > 1)
    End_Chord_PC = 1;
end

Load.Precomputed_Data.Span_Length = Get_Point_Line_Distance(Load.Chord_Surface.A2_projected - (Load.Chord_Surface.A2_projected - Load.Chord_Surface.A4_projected)*End_Chord_PC, Load.Chord_Surface.A1_projected, Load.Chord_Surface.A3_projected);

% Погонная нагрузка на корневой хорде
Load.Precomputed_Data.Linear_Load_Root = (2 - 3*Load.Precomputed_Data.Span_Pressure_Center)*2*Load.Pn/Load.Precomputed_Data.Span_Length;

% Погонная нагрузка на концевой хорде
Load.Precomputed_Data.Linear_Load_End = (3*Load.Precomputed_Data.Span_Pressure_Center - 1)*2*Load.Pn/Load.Precomputed_Data.Span_Length;

% Разница погонной нагрузки на корневой и на концевой хорде
Load.Precomputed_Data.Linear_Load_Difference = Load.Precomputed_Data.Linear_Load_Root - Load.Precomputed_Data.Linear_Load_End;
